package cn.david.pet.controller;


import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.LoginContext;
import cn.david.basic.util.PageList;

import cn.david.pet.domain.Pet;
import cn.david.pet.domain.PetDetail;
import cn.david.pet.query.PetQuery;
import cn.david.pet.service.IPetService;
import cn.david.user.domian.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    /**
     * 查询一个  localhost:8080/pet/findone  1
     * localhost:8080/pet/1
     * @return
     */
    @GetMapping("/detail/{id}")
    public PetDetail findDetailByPetId(@PathVariable("id") Long id){
        PetDetail detail = petService.loadByIdDetail(id);
        return detail;
    }

    /**
     * 查询所有localhost:8080/pet
     * @return
     */
    @GetMapping

    public List<Pet> findAll(){
        List<Pet> list = petService.getAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/pet*/
    public AjaxResult addorUpdate(@RequestBody Pet pet){
        try {
            if (pet.getId()==null){//添加
                petService.add(pet);
            }else{//修改
                petService.upadte(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/pet/4*/

    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            petService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PostMapping
    public PageList<Pet> queryForPage(@RequestBody PetQuery petQuery){

        PageList<Pet> pageList = petService.queryData(petQuery);
        return pageList;
    }
    @PostMapping("/list")
    public PageList<Pet> queryList(@RequestBody PetQuery petQuery){
        petQuery.setState(1);
        PageList<Pet> pageList = petService.queryData(petQuery);
        return pageList;
    }




    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids){
        /*链式编程思维*/
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (BusinessExcepiton e){
            return AjaxResult.me().setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }
    @PatchMapping("/fnSale")
    public AjaxResult fnSale(@RequestBody Long[] ids){
        /*链式编程思维*/
        try {
            petService.fnSale(ids);
            return AjaxResult.me();
        } catch (BusinessExcepiton e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }
    @GetMapping("/{id}")
    public Pet getDeail(@PathVariable("id") Long id){
        return petService.getById(id);

    }
    @GetMapping("/adpot/{id}")
    public AjaxResult adpot(@PathVariable("id") Long id , HttpServletRequest request){

        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            petService.adpot(id,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("领养失败"+e.getMessage());
        }

    }
}
